package com.szxy.plugin;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

/**
 * @Author: zwz
 * @Date:2020/7/27 15:59
 * @Description:com.szxy.plugin
 * @Version:1.0
 * 自定义渲染策略
 **/
public class AuthorPluginTablePolicy extends DynamicTableRenderPolicy {

    // 作者填充数据所在行数
    int goodsStartRow = 2;

    @Override
    public void render(XWPFTable table, Object data) {
        // 数据校验
        if (null == data) return;
        // 获取所有作者数据
        List<RowRenderData> authors = (List<RowRenderData>) data;
        // 删除当前行
        table.removeRow(goodsStartRow);
        // 数据校验
        if (null != authors) {
            for (int i = 0; i < authors.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(goodsStartRow);
                for (int j = 0; j < 4; j++)
                    insertNewTableRow.createCell();
                MiniTableRenderPolicy.renderRow(table, goodsStartRow, authors.get(i));
                goodsStartRow++;
            }
        }
    }
}
